import 'package:flutter/material.dart';
import 'package:sweet_app/bloc/application_bloc.dart';
import 'package:sweet_app/bloc/bloc_provider.dart';
import 'package:sweet_app/buttons/color_loader_5.dart';
import 'package:sweet_app/buttons/dot_type.dart';
import 'package:sweet_app/main.dart';
import 'package:sweet_app/pages/chat/chat_page.dart';
import 'package:sweet_app/pages/chat/history_page.dart';
import 'package:sweet_app/pages/details/details_page.dart';
import 'package:sweet_app/pages/friends/friends_page.dart';
import 'package:sweet_app/pages/login/login.dart';
import 'package:sweet_app/pages/register/register_page.dart';
import 'package:sweet_app/pages/test_page/custom_paint.dart';
import 'package:sweet_app/pages/test_page/gradient_circular_progress.dart';
import 'package:sweet_app/pages/test_page/test_paint.dart';
import 'package:sweet_app/pages/test_page/turn_box_page.dart';
import 'package:sweet_app/pages/widgets/chat_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _currentIndex = 0;
  PageController _controller = PageController();

  @override
  Widget build(BuildContext context) {
    final ApplicationBloc applicationBloc =
        BlocProvider.of<ApplicationBloc>(context);
    return StreamBuilder<int>(
      builder: (context, snapshotData) {
        if (snapshotData.hasData) {
          switch (snapshotData.data) {
            case AppEvent.login:
              return LoginPage();
            case AppEvent.register:
              return RegisterPage();
            case AppEvent.home:
              return Scaffold(
                body: PageView(
                  controller: _controller,
                  onPageChanged: _onTap,
                  children: <Widget>[
                    StackPage(child: HistoryPage()),
                    StackPage(child: FriendsPage()),
                    StackPage(child: DetailsPage())
                  ],
                ),
                bottomNavigationBar: BottomNavigationBar(
                  items: [
                    BottomNavigationBarItem(
                        icon: Icon(Icons.message), title: Text("聊天")),
                    BottomNavigationBarItem(
                        icon: Icon(Icons.people), title: Text("朋友")),
                    BottomNavigationBarItem(
                        icon: Icon(Icons.person), title: Text("我")),
                  ],
                  onTap: _onTap,
                  currentIndex: _currentIndex,
                ),
              );
            default:
              return Container(width: 0.0, height: 0.0);
          }
        } else {
          return Container(
            color: Colors.white,
            child: ColorLoader5(
              dotOneColor: Colors.redAccent,
              dotTwoColor: Colors.blueAccent,
              dotThreeColor: Colors.green,
              dotType: DotType.circle,
              dotIcon: Icon(Icons.adjust),
              duration: Duration(seconds: 1),
            ),
          );
        }
      },
      stream: applicationBloc.appEventStream,
    );
  }

  void _onTap(int value) {
    setState(() {
      _currentIndex = value;
      _controller.animateToPage(value,
          duration: Duration(milliseconds: 10), curve: Curves.ease);
    });
  }


}

class ItemWidget extends StatelessWidget {
  final VoidCallback onTap;
  final String name;

  ItemWidget({this.name, this.onTap});

  @override
  Widget build(BuildContext context) {
    return Flexible(
        child: InkWell(
      onTap: onTap,
      child: Card(
        color: Colors.blueAccent,
        child: Container(
          padding: const EdgeInsets.all(60.0),
          child: Text(name),
        ),
      ),
    ));
  }
}
